<template>
  <el-card>
    <el-input
      placeholder="请输入姓名"
      v-model="name"
      clearable
      @clear="getListData"
    ></el-input>
    <el-select v-model="value" placeholder="" @clear="getListData" clearable>
      <el-option
        v-for="item in gender"
        :key="item.value"
        :label="item.label"
        :value="item.value"
        :disabled="item.disabled"
      />
    </el-select>
    <el-button type="primary" @click="getListData">查询</el-button>
    <el-button type="primary" @click="handleAddUser">新增用户</el-button>
    <el-upload
      limit="1"
      :show-file-list="false"
      action="api/user/user-import"
      :headers="uploadHeaders"
    >
      <el-button>导入excel</el-button>
    </el-upload>
    <el-button @click="onToExcelClick">导出excel</el-button>

    <export-to-excel v-model="exportToExcelVisible"></export-to-excel>
  </el-card>
  <el-card>
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column label="" type="index" />
      <el-table-column prop="username" label="姓名"> </el-table-column>
      <el-table-column prop="birthday" label="生日"> </el-table-column>
      <el-table-column prop="gender" label="性别">
        <template #default="{ row }">
          {{ row.gender === 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间"> </el-table-column>
      <el-table-column label="操作" fixed="right" width="260">
        <template #default="{ row }">
          <el-button type="primary" size="mini" @click="handleEdit(row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="handleDelete(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 20, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script setup>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { getUserList, deleteUser } from '@/api/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import ExportToExcel from './components/Export2Excel.vue'

const tableData = ref([])
const total = ref(0)
const pageIndex = ref(1)
const pageSize = ref(10)
const loading = ref(false)
// 检索字段
const name = ref('')
const value = ref('')
const gender = [
  {
    value: 0,
    label: '女'
  },
  {
    value: 1,
    label: '男'
  }
]

const getListData = async () => {
  loading.value = true
  await getUserList({
    page: pageIndex.value,
    pageSize: pageSize.value,
    username: name.value,
    gender: value.value
  })
    .then((res) => {
      tableData.value = res.items || []
      total.value = res.total
    })
    .finally(() => {
      loading.value = false
    })
}
getListData()

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  pageSize.value = currentSize
  getListData()
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  pageIndex.value = currentPage
  getListData()
}
// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定删除用户吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    ype: 'warning'
  })
    .then(() => {
      const { id } = row
      deleteUser({
        id
      })
        .then((res) => {
          getListData()
        })
        .finally(() => {})
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
    })
    .catch(() => {})
}

const router = useRouter()
const handleAddUser = () => {
  router.push('/user/userAdd')
}
const handleEdit = (row) => {
  router.push('/user/userEdit')
}

/**
 * excel 导入
 */
const uploadHeaders = ref({})
const store = useStore()
uploadHeaders.value = {
  token: store.getters.token,
  'Content-Type': 'multipart/form-data'
}
/**
 * excel 导出点击事件
 */
const exportToExcelVisible = ref(false)
const onToExcelClick = () => {
  exportToExcelVisible.value = true
}
</script>
<style scoped lang="scss">
.pagination {
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
}

.el-input {
  width: 200px;
  margin-right: 10px;
}
</style>
